/*
 * Copyright 2012 Jonathan St-Michel <jonathan@abiwebz.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.abiwebz.office.rm.database.util;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import org.openide.WizardDescriptor;

/**
 *
 * @author Jonathan St-Michel <jonathan@abiwebz.net>
 */
public abstract class WizardValidate implements PropertyChangeListener {

    protected boolean isValid = false;
    protected final EventListenerList listeners = new EventListenerList();
    protected WizardDescriptor model = null;
    
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        boolean oldState = isValid;
        isValid = check();
        fireChangeEvent(this, oldState, isValid);
    }
    
    protected final void fireChangeEvent(Object source, boolean oldState, boolean newState){
        if(oldState != newState){
            ChangeEvent ev = new ChangeEvent(source);
            for(ChangeListener listener : listeners.getListeners(ChangeListener.class)){
                listener.stateChanged(ev);
            }
        }
    }
    
    public void addChangeListener(ChangeListener l) {
        listeners.add(ChangeListener.class, l);
    }

    public void removeChangeListener(ChangeListener l) {
        listeners.remove(ChangeListener.class, l);
    }
    
    protected void setInformation(String message){
        if(this.model != null){
            model.getNotificationLineSupport().setInformationMessage(message);
        }
    }
    
    protected void setWarning(String message){
        if(this.model != null){
            model.getNotificationLineSupport().setWarningMessage(message);
        }
    }
    
    protected void setError(String message){
        if(this.model != null){
            model.getNotificationLineSupport().setErrorMessage(message);
        }
    }
    
    protected void clearMessage(){
        if(this.model != null){
            model.getNotificationLineSupport().setInformationMessage(null);
        }
    }
    
    public abstract boolean check();
    
}
